<?php
/**
 * Created by PhpStorm.
 * User: 69548
 * Date: 2018/7/28
 * Time: 10:17
 */

namespace app\wap\controller;

use app\wap\model\Comment as M;
use think\Controller;


class Comment extends Controller
{

    public function _initialize()
    {
        $uid=input('uid');
        $member_id=input('member_id');
        if  (!empty($uid)){
            $this->member_pcode=  $uid;
            $uids=db('member')->where('member_id',$uid)->find();
            if (!empty($member_id) && $uid != $member_id && $uids['member_pcode'] != $member_id ){
                db('member')->where('member_id',$member_id)->update([
                    'member_pcode'=> $uid
                ]);
            }
        }
    }

    public function index(){

        header('Access-Control-Allow-Origin:*');
      $goods_id=  input('goods_id');

        $shu=[];
            $m=new M();
            if (!empty($goods_id)){
                $comment=$m
                    ->alias('a')
                    ->join('member d','a.member_id=d.member_id')
                    ->join('attachment b','b.id=d.thumb')
                    ->where('a.goods_id',$goods_id)
                    ->where('a.comment_status',2)
                    ->field('a.content,a.creattime,d.member_nickname,b.filepath as touxiang,a.comment_img')
                    ->order('a.creattime desc')
                    ->select();
                foreach ($comment as $k=> $v){

                    $comment[$k]['creattime']= date("Y-m-d H:i:s",$v['creattime']);
//                    $img=!empty($v['comment_img'])?explode(',',$v['comment_img']):'';
//                    if (empty($comment[$k]['comment_img'])){
                        $comment[$k]['comment_img']= json_decode($v['comment_img']);
//                    }else{
//                        $comment[$k]['comment_img']= '';
//                    }
                }
                if ($comment==true){
                    $shu=[
                        'code'=>0,
                        'msg'=>'ok',
                        'data'=>$comment
                    ];
                }else{
                    $shu=[
                        'code'=>101,
                        'msg'=>'还没有评论哦!',
                    ];
                }
            }else{
                $shu=[
                    'code'=>101,
                    'msg'=>'参数错误',
                ];
            }
        return json_encode($shu);

    }



    public function comments(){





    }

















}